package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * 政务服务事项基本目录
 */

public class ZhengWuFuWuList extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return this.parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {
        //当前页
        int currentPage = 1;
        //总页数
        int pages = 0;
        while (currentPage != pages){
            String url = "https://wssp.hainan.gov.cn/wssp3/basiccontents/getBasicSxList";
            JSONObject header = new JSONObject();
            header.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            header.put("Referer", "https://wssp.hainan.gov.cn/hnwt/jiben-detail");
            JSONObject parmas = new JSONObject();
            parmas.put("pageNum", currentPage);
            parmas.put("pageSize", "10");
            String strResponse = postFormReq(url,header,parmas);
            JSONObject response = JSONObject.parseObject(strResponse);
            for (Object o : response.getJSONObject("data").getJSONArray("list")){
                JSONObject list = (JSONObject) o;
                JSONObject result = getPageInfo(list);
                System.out.println(currentPage+"\n"+result);//测试用代码
                return processResult(
                        null,
                        null,
                        null,
                        null,
                        result,
                        "海南省",
                        "政务公开-政务服务事项基本目录"
                );
            }
            //更新当前页和总页数
            currentPage++;
            pages = response.getJSONObject("data").
                    getJSONObject("page").getInteger("pages") + 1;
        }
        return null;
    }

    public JSONObject getPageInfo(JSONObject list){
        JSONObject result = new JSONObject();
        result.put("基本编码", list.getString("catalogcode"));
        result.put("事项名称", list.getString("taskname"));

        switch (list.getString("tasktype")){
            case "1":
                result.put("事项类型", "行政许可");
                break;
            case "2":
                result.put("事项类型", "行政处罚");
                break;
            case "3":
                result.put("事项类型", "行政强制");
                break;
            case "4":
                result.put("事项类型", "行政征收");
                break;
            case "5":
                result.put("事项类型", "行政给付");
                break;
            case "6":
                result.put("事项类型", "行政检查");
                break;
            case "7":
                result.put("事项类型", "行政确认");
                break;
            case "8":
                result.put("事项类型", "行政奖励");
                break;
            case "9":
                result.put("事项类型", "行政裁决");
                break;
            case "10":
                result.put("事项类型", "其他行政权力");
                break;
            case "20":
                result.put("事项类型", "公共服务");
                break;
            default:
                result.put("事项类型", "无");
        }

        result.put("设定依据", list.getString("bylaw"));

        switch (list.getString("uselevel")){
            case "1":
                result.put("行使层级", "国家级/局（署、会）");
                break;
            case "2":
                result.put("行使层级", "省级/直属");
                break;
            case "3":
                result.put("行使层级", "市级/隶属");
                break;
            case "4":
                result.put("行使层级", "县级");
                break;
            case "5":
                result.put("行使层级", "镇（乡、街道）级");
                break;
            case "6":
                result.put("行使层级", "村（社区）级");
                break;
            case "7":
                result.put("行使层级", "分级管理");
                break;
            default:
                result.put("行使层级", "无");
        }

        result.put("中央业务指导部门", list.getString("funcode"));
        return result;
    }

    public static void main(String[] args) {
        ZhengWuFuWuList zhengWuFuWuList = new ZhengWuFuWuList();
        zhengWuFuWuList.getDataDemo();
    }
}
